﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;
using System.IO;
using OnlineDigitalCameraWebsite.Class;

namespace OnlineDigitalCameraWebsite.Admin
{
    public partial class LensList_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 

                string type = Request.QueryString["type"].ToString();
                string id = Request.QueryString["id"].ToString();
                if (type == "them")
                {
                    lblTieuDe.Text = "Thêm lens";
                    lblTieuDe.DataBind();
                }
                else
                {
                    lblTieuDe.Text = "Cập Nhật lens";
                    lblTieuDe.DataBind();
                    lblLink.Text = "Xem chi tiết lens";
                    lblLink.NavigateUrl = "/ChiTiet.aspx?cate=lens&id=" + id;
                    lblTieuDe.DataBind();
                    LoadThongTin(id);
                }
            }
        }
        LensBUS acBUS = new LensBUS();
        private void LoadThongTin(string id)
        {
            int idma = int.Parse(id);
            Lens tt = new Lens();
            tt = acBUS.GetById(idma);
            txtName.Text = tt.lensNames;
            txtdes.Text = tt.lensDetails;
            txtlensFocallensgth.Text = tt.lensFocallensgth;
            txtlensMaxaperture.Text = tt.lensMaxaperture;
            txtlensMinaperture.Text = tt.lensMinaperture;
            txtlensMinfocus.Text = tt.lensMinfocus;
            txtlensMaxmagnification.Text = tt.lensMaxmagnification;
            txtprice.Text = tt.lensPrice.ToString();
            imgHinhAnh.ImageUrl = tt.lensImages;
            ddlStatus.SelectedValue = tt.lensStatus.ToString();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string type = Request.QueryString["type"].ToString();
                string id = Request.QueryString["id"].ToString();
                if (type == "them")
                {
                    Lens a = new Lens();
                    a.lensNames = txtName.Text;
                    string hinhanh = "";
                    if (FileUpload1.HasFile)
                    {
                        string ext = Path.GetExtension(FileUpload1.FileName);
                        string name = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
                        string ten = "/Uploads/images/camera_moi/" + name + "_" + XuLy.GetDateTimeNow() + ext;
                        //FileInfo Image = new FileInfo(Server.MapPath(ten));
                        //if (!Image.Exists)
                        //{
                        FileUpload1.SaveAs(Server.MapPath(ten));
                        hinhanh = ten;
                        //}
                    }
                    else
                    {
                        hinhanh = "/Uploads/images/no_image.png";
                    }
                    a.lensImages = hinhanh;
                    a.lensFocallensgth = txtlensFocallensgth.Text;
                    a.lensDetails = txtdes.Text;
                    a.lensMaxaperture = txtlensMaxaperture.Text;
                    a.lensMinaperture = txtlensMinaperture.Text;
                    a.lensMinfocus = txtlensMinfocus.Text;
                    a.lensMaxmagnification = txtlensMaxmagnification.Text;
                    a.lensPrice = double.Parse(txtprice.Text);
                    a.lensStatus = bool.Parse(ddlStatus.SelectedValue.ToString());
                    acBUS.Insert(a);
                    Response.Redirect("/Admin/LensList.aspx");
                }
                else
                {

                    Lens a = new Lens();
                    int idma = int.Parse(Request.QueryString["id"].ToString());
                    a = acBUS.GetById(idma);
                    a.lensNames = txtName.Text;
                    string hinhanh = "";
                    if (FileUpload1.HasFile)
                    {
                        string ext = Path.GetExtension(FileUpload1.FileName);
                        string name = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
                        string ten = "/Uploads/images/camera_moi/" + name + "_" + XuLy.GetDateTimeNow() + ext;
                        //FileInfo Image = new FileInfo(Server.MapPath(ten));
                        //if (!Image.Exists)
                        //{
                        FileUpload1.SaveAs(Server.MapPath(ten));
                        hinhanh = ten;
                        //}
                    }
                    else
                    {
                        hinhanh = imgHinhAnh.ImageUrl;
                    }
                    a.lensImages = hinhanh;
                    a.lensFocallensgth = txtlensFocallensgth.Text;
                    a.lensDetails = txtdes.Text;
                    a.lensMaxaperture = txtlensMaxaperture.Text;
                    a.lensMinaperture = txtlensMinaperture.Text;
                    a.lensMinfocus = txtlensMinfocus.Text;
                    a.lensMaxmagnification = txtlensMaxmagnification.Text;
                    a.lensPrice = double.Parse(txtprice.Text);
                    a.lensStatus = bool.Parse(ddlStatus.SelectedValue.ToString());
                    acBUS.Edit(a);
                    lbThongBao.Text = "Cập nhật thành công";
                    lbThongBao.Visible = true;
                    LoadThongTin(id);
                }
            }
            catch (Exception ex)
            {
                lbThongBao.Text = "Lỗi: " + ex.Message;
                lbThongBao.Visible = true;
            }
        }
    }
}